<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/10/29
 * Time: 14:22
 */
namespace App\Service\Live;

use App\Constant\RedisKeyConstant;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;
use App\Http\Model\MongoDB\LiveList;

class AlterService extends BaseService
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * client user thumb up
     * @param string $room_name
     * @param string $user_id
     * @return mixed
     */
    public function userThumb($room_name = '', $user_id = '')
    {
        return Redis::HINCRBY(RedisKeyConstant::ROOM_THUMB_UP_PREFIX . $room_name, $user_id, 1);
    }

    /**
     * get room total thumb up
     * @param string $room_name
     * @return float|int
     */
    public function getThumb($room_name = '')
    {
        $total_values   =   Redis::HVALS(RedisKeyConstant::ROOM_THUMB_UP_PREFIX . $room_name);
        return array_sum($total_values);
    }

    /**
     * get user all thumb up         (for recommendation goods to user in group shop / recommendation anchors to user in live)
     * @param string $user_id
     * @param array $room_names
     * @return array
     */
    public function getUser($user_id = '', $room_names = [])
    {
        if (empty($room_names)) {
            return [];
        }

        $result =   [];
        foreach ($room_names as $room_name) {
            $room_thumb =   Redis::HGET(RedisKeyConstant::ROOM_THUMB_UP_PREFIX . $room_name, $user_id);
            $result[$room_name]    =   empty($room_thumb) ? 0 : $room_thumb;
        }
        return $result;
    }

    /**
     * set user focus
     * @param string $user_id
     * @param string $room_id
     * @return array
     */
    public function userFocus($user_id = '', $room_id = '')
    {
        /**
         * get anchor id by room id
         */
        $room_info  =   LiveList::where(['Id' => $room_id])->select('CreatedBy', 'Id')->first();
        if (empty($room_info)) {
            return [false, []];
        }
        $anchor_id  =   $room_info->CreatedBy;

        Redis::SADD(RedisKeyConstant::USER_FOCUS_PREFIX . $user_id, $anchor_id);
        Redis::SADD(RedisKeyConstant::ANCHOR_FOLLOWERS_PREFIX . $anchor_id, $user_id);
        $user_nick  =   Redis::HGET(RedisKeyConstant::USER_DETAIL_INFO, $user_id);
        return [true, ['anchor_id' => $anchor_id, 'user_info' => empty($user_nick) ? [] : json_decode($user_nick, true)]];
    }

    /**
     * cancel anchor focus
     * @param string $user_id
     * @param string $room_id
     * @return array
     */
    public function cancelFocus($user_id = '', $room_id = '')
    {
        /**
         * get anchor id by room id
         */
        $room_info  =   LiveList::where(['Id' => $room_id])->select('CreatedBy', 'Id')->first();
        if (empty($room_info)) {
            return [false, []];
        }
        $anchor_id  =   $room_info->CreatedBy;

        Redis::SREM(RedisKeyConstant::USER_FOCUS_PREFIX . $user_id, $anchor_id);
        Redis::SREM(RedisKeyConstant::ANCHOR_FOLLOWERS_PREFIX . $anchor_id, $user_id);
        $user_nick  =   Redis::HGET(RedisKeyConstant::USER_DETAIL_INFO, $user_id);
        return [true, ['anchor_id' => $anchor_id, 'user_info' => empty($user_nick) ? [] : json_decode($user_nick, true)]];
    }

    /**
     * get all total focus
     * @param string $room_id
     * @return int
     */
    public function totalFocus($room_id = '')
    {
        $room_info  =   LiveList::where(['Id' => $room_id])->select('CreatedBy', 'Id')->first();
        if (empty($room_info)) {
            return 0;
        }
        $anchor_id  =   $room_info->CreatedBy;
        return Redis::SCARD(RedisKeyConstant::ANCHOR_FOLLOWERS_PREFIX . $anchor_id);
    }

    /**
     * check user if focus
     * @param string $user_id
     * @param string $room_id
     * @return array
     */
    public function ifUserFocus($user_id = '', $room_id = '')
    {
        /**
         * get anchor id by room id
         */
        $room_info  =   LiveList::where(['Id' => $room_id])->select('CreatedBy', 'Id')->first();
        if (empty($room_info)) {
            return [false, []];
        }
        $anchor_id  =   $room_info->CreatedBy;

        $focus_result   =    Redis::SISMEMBER(RedisKeyConstant::ANCHOR_FOLLOWERS_PREFIX . $anchor_id, $user_id);
        if ($focus_result) {
            return [true, ['user_id' => $user_id, 'anchor_id' => $anchor_id]];
        } else {
            return [false, []];
        }
    }

    /**
     * user fetch coupon
     * @param string $user_id
     * @param string $coupon_id
     * @param string $room_name
     * @return array
     */
    public function userFetch($user_id = '', $coupon_id = '', $room_name = '')
    {
        if (empty($coupon_id)) {
            return [false, '优惠券不能为空'];
        }

        Redis::SADD(RedisKeyConstant::COUPON_FETCH_PREFIX . $room_name . '_' . $coupon_id, $user_id);
        return [true, '领取成功'];
    }

    /**
     * user share room
     * @param string $room_name
     * @param string $user_id
     * @return mixed
     */
    public function userShare($room_name = '', $user_id = '')
    {
        return Redis::SADD(RedisKeyConstant::SHARE_FETCH_PREFIX . $room_name, $user_id);
    }

}
